/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iffmyexp;

/**
 *
 * @author rory
 */
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class InputValidator implements Validator {

    static String min;
    static String max;
    String length;

    public static void setMin(String m) {
        min = m;
    }

    public static void setMax(String m) {
        max = m;
    }

    public void validate(FacesContext context, UIComponent component, Object value)
            throws ValidatorException {

        String email = (String) value;

        /*if (!email.contains(min)) {
        FacesMessage message = new FacesMessage();
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        message.setSummary("Input is not valid.");
        message.setDetail("Input is not valid.");
        context.addMessage("userForm:Email", message);
        throw new ValidatorException(message);
        }*/

        try {
            int x = Integer.parseInt(min);
            int y = Integer.parseInt(max);
            int in = Integer.parseInt(email);

            if (in < x || in > y) {

                FacesMessage message = new FacesMessage();
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                message.setSummary("Input is not valid.");
                message.setDetail("Input is not valid.");
                context.addMessage("userForm:Email", message);
                throw new ValidatorException(message);

            }
        } catch (Exception e) {
            FacesMessage message = new FacesMessage();
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            message.setSummary("Input is not valid.");
            message.setDetail("Input is not valid.");
            context.addMessage("userForm:Email", message);
            throw new ValidatorException(message);
        }
    }
}
